package com.zzzy.doc.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzzy.doc.dao.entity.DocUserOperateLink;
import com.zzzy.doc.dao.mapper.DocUserOperateLinkMapper;
import com.zzzy.framework.common.util.SecurityUtils;
import com.zzzy.framework.core.mybatis.query.LambdaQueryWrapperE;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 公文用户操作关联表
 *
 * @author fc
 * @since 2024-08-12
 */
@Service
public class DocUserOperateLinkService extends ServiceImpl<DocUserOperateLinkMapper, DocUserOperateLink> {

    public void addByDocId(Long docId) {
        if (!isLoginUserLinkByDocId(docId)){
            DocUserOperateLink docUserOperateLink = new DocUserOperateLink();
            docUserOperateLink.setDocId(docId);
            docUserOperateLink.setUserId(SecurityUtils.getLoginUser().getId());
            baseMapper.insert(docUserOperateLink);
        }
    }

    /**
     * 当前用户是否是合同经办人
     */
    public boolean isLoginUserLinkByDocId(Long docId) {
        Long count = baseMapper.selectCount(new LambdaQueryWrapperE<DocUserOperateLink>().eq(DocUserOperateLink::getDocId, docId)
                .eq(DocUserOperateLink::getUserId, SecurityUtils.getLoginUser().getId()));
        return count > 0;
    }

    public boolean isContainsLink(Long docId, Set<Long> userIdSet) {
        Long count = baseMapper.selectCount(new LambdaQueryWrapperE<DocUserOperateLink>().eq(DocUserOperateLink::getDocId, docId)
                .in(DocUserOperateLink::getUserId, userIdSet));
        return count > 0;
    }

    public List<Long> getOperateLinkUserIdListByDocId(Long docId) {
        List<DocUserOperateLink> operateLinkList = baseMapper.selectList(new LambdaQueryWrapperE<DocUserOperateLink>().eq(DocUserOperateLink::getDocId, docId));
        return operateLinkList.stream().map(DocUserOperateLink::getUserId).collect(Collectors.toList());
    }
}
